﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dpdw.Behaviour.StatePattern
{
    /// <summary>
    /// 糖果机
    /// 投入25分钱则出售糖果
    /// </summary>
    public class GumballMachine
    {
        IState soldOutState;
        IState noQuarterState;
        IState hasQuarterState;
        IState soldState;

        IState state;
        int count = 0;

        public GumballMachine(int numberGumballs)
        {
            soldOutState = new SoldOutState(this);
            noQuarterState = new NoQuarterState(this);
            hasQuarterState = new HasQuarterState(this);
            soldState = new SoldState(this);

            this.count = numberGumballs;
            if (this.count > 0)
            {
                state = noQuarterState;
            }
        }

        #region Properties

        /// <summary>
        /// 糖果机状态
        /// </summary>
        public IState State
        {
            get
            {
                return this.state;
            }
            set
            {
                this.state = value;
            }
        }

        /// <summary>
        /// 糖果数量
        /// </summary>
        public int Count
        {
            get
            {
                return this.count;
            }
        }

        public IState SoldOutState
        {
            get
            {
                return this.soldOutState;
            }
        }

        public IState NoQuarterState
        {
            get
            {
                return this.noQuarterState;
            }
        }

        public IState HasQuarterState
        {
            get
            {
                return this.hasQuarterState;
            }
        }

        public IState SoldState
        {
            get
            {
                return this.soldState;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// 投币
        /// </summary>
        public void InsertQuarter()
        {
            state.InsertQuarter();
        }

        /// <summary>
        /// 退币
        /// </summary>
        public void EjectQuarter()
        {
            state.EjectQuarter();
        }

        /// <summary>
        /// 转动句柄
        /// </summary>
        public void TurnCrank()
        {
            state.TurnCrank();
        }

        /// <summary>
        /// 发放糖果
        /// </summary>
        public void Dispense()
        {
            state.Dispense();
        }

        /// <summary>
        /// 减少糖果
        /// </summary>
        public void ReleaseBall()
        {
            System.Diagnostics.Trace.WriteLine("GumballMachine" + " " + "ReleaseBall");

            if (this.count != 0)
            {
                this.count--;
            }
        }

        #endregion
    }
}
